package test_achieve.web;

import constant.Constant;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import util.ConfigSql;
import util.JdbcUtil;
import util.tools.BusinessTools;
import util.tools.CommonTools;

/**
 * Created by Administrator on 2018/6/8 0008.
 */
public class CouponShareWeb {
    @Given("^进入红包分享页面：(.*)$")
    public void 进入红包分享页面(String type) throws Throwable {
        switch (type){
            case "直减":
                Constant.driverWeb.get(Constant.CouponShareZhijianURL);
                break;
            case "满减":
                Constant.driverWeb.get(Constant.CouponShareManjianURL);
                break;
            case "一口价":
                Constant.driverWeb.get(Constant.CouponShareYikoujiaURL);
                break;
            case "折扣卡":
                Constant.driverWeb.get(Constant.CouponShareZhekouURL);
                break;
        }
    }

    @Given("^输入并领取优惠券：(.*)$")
    public void 输入并领取优惠券(String userMobileNum) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        Constant.driverWeb.findElement(By.xpath("/html/body/div[4]/div[1]/div[1]/p[1]/input")).sendKeys(userMobileNum);
        Boolean isCaptcha = CommonTools.checkElementExit(Constant.driverWeb,By.xpath("/html/body/div[4]/div[1]/div[1]/p[2]/input"));

//        CommonTools.checkElementExit(Constant.driverWeb,By.className("tel-num-input js-img-code"));
        if (isCaptcha){
            Constant.driverWeb.findElement(By.xpath("/html/body/div[4]/div[1]/div[1]/p[2]/img")).click();
            String captcha = BusinessTools.getH5GraphicalCaptcha(Constant.driverWeb,"_wx_Coupon");
            Constant.driverWeb.findElement(By.xpath("/html/body/div[4]/div[1]/div[1]/p[2]/input")).sendKeys(captcha);
        }
        Constant.driverWeb.findElement(By.xpath("/html/body/div[4]/div[1]/div[1]/button")).click();
    }

    @Given("^判断已领取优惠券类型：(.*)：(.*)$")
    public void 判断已领取优惠券类型(String type,String counts) throws Throwable {
        String pageType = Constant.driverWeb.findElement(By.className("name")).getText();
        Assert.assertEquals(type,pageType);
        String pageCoun = Constant.driverWeb.findElement(By.className("num")).getText();
        Assert.assertEquals(pageCoun,counts);


    }

    @Given("^返回到领取页面$")
    public void 返回到领取页面() throws Throwable {
        Constant.driverWeb.navigate().refresh();

    }


    @Given("^显示超出领取提醒$")
    public void 显示超出领取提醒() throws Throwable {
        String tip= Constant.driverWeb.findElement(By.className("tips binded js-tips")).getText();
        Assert.assertEquals("您已经领取过这个红包啦~",tip);
    }

    @Given("^清除红包裂变领取限制：(.*)$")
    public void 清除红包裂变领取限制(String mobile) throws Throwable {
        BusinessTools.removeCouponRedis(mobile);
        String mobileSecret = BusinessTools.getMobileSecret(mobile);
        JdbcUtil.sqlExecute(ConfigSql.get("deleMoblieCoupon",mobileSecret,"9099500","17072388"));
    }
}
